﻿using System;
using System.Collections.Generic;
using NATTraversal;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.Networking.Match;
using UnityEngine.Networking.NetworkSystem;

// Token: 0x020000AA RID: 170
[HelpURL("http://grabblesgame.com/nat-traversal/docs/class_n_a_t_traversal_1_1_network_manager.html")]
public class ExampleNetworkManager : NATTraversal.NetworkManager
{
	// Token: 0x060003F6 RID: 1014 RVA: 0x00020DCC File Offset: 0x0001EFCC
	public override void OnMatchList(bool success, string extendedInfo, List<MatchInfoSnapshot> matchList)
	{
		int count = matchList.Count;
		if (!success)
		{
			Debug.Log("Failed to retrieve match list");
			return;
		}
		if (count == 0)
		{
			Debug.Log("Match list is empty");
			return;
		}
		MatchInfoSnapshot matchInfoSnapshot = matchList[0];
		if (matchInfoSnapshot == null)
		{
			Debug.Log("Match list is empty");
			return;
		}
		Debug.Log("Found a match, joining");
		this.matchID = matchInfoSnapshot.networkId;
		this.StartClientAll(matchInfoSnapshot, null, string.Empty, 0, 0, false);
	}

	// Token: 0x060003F7 RID: 1015 RVA: 0x00020E44 File Offset: 0x0001F044
	private void OnGUI()
	{
		if (GUI.Button(new Rect(10f, 10f, 150f, 100f), "Host"))
		{
			if (this.matchMaker == null)
			{
				this.matchMaker = base.gameObject.AddComponent<NetworkMatch>();
			}
			base.StartMatchMaker();
			this.StartHostAll("Hello World", (!base.customConfig) ? this.matchSize : ((uint)(base.maxConnections + 1)), true, string.Empty, 0, 0, 0, null);
		}
		if (GUI.Button(new Rect(10f, 110f, 150f, 100f), "Join"))
		{
			if (this.matchMaker == null)
			{
				this.matchMaker = base.gameObject.AddComponent<NetworkMatch>();
			}
			base.StartMatchMaker();
			this.matchMaker.ListMatches(0, 10, string.Empty, true, 0, 0, new NetworkMatch.DataResponseDelegate<List<MatchInfoSnapshot>>(this.OnMatchList));
		}
		if (GUI.Button(new Rect(10f, 210f, 150f, 100f), "Disconnect"))
		{
			if (NetworkServer.active)
			{
				NetworkServer.SetAllClientsNotReady();
				this.StopHost();
			}
			else
			{
				this.StopClient();
			}
		}
		if (NetworkServer.active && GUI.Button(new Rect(10f, 310f, 150f, 100f), "Send To All"))
		{
			NetworkServer.SendToAll(global::MsgType.OtherTestMessage, new EmptyMessage());
		}
		GUI.Label(new Rect(10f, 410f, 300f, 30f), "Is connected to Facilitator: " + this.natHelper.isConnectedToFacilitator);
		if (NetworkClient.active && !NetworkServer.active)
		{
			if (this.directClient != null && this.directClient.isConnected)
			{
				GUI.Label(new Rect(10f, 450f, 300f, 30f), "Client connected directly");
			}
			else if (this.punchthroughClient != null && this.punchthroughClient.isConnected)
			{
				GUI.Label(new Rect(10f, 450f, 300f, 30f), "Client connected via punchthrough");
			}
			else if (this.relayClient != null && this.relayClient.isConnected)
			{
				GUI.Label(new Rect(10f, 450f, 300f, 30f), "Client connected via relay");
			}
		}
	}

	// Token: 0x060003F8 RID: 1016 RVA: 0x000210E3 File Offset: 0x0001F2E3
	public override void OnDoneConnectingToFacilitator(ulong guid)
	{
		if (guid == 0UL)
		{
			Debug.Log("Failed to connect to Facilitator");
		}
		else
		{
			Debug.Log("Facilitator connected");
		}
	}

	// Token: 0x060003F9 RID: 1017 RVA: 0x00021106 File Offset: 0x0001F306
	private void OnTestMessage(NetworkMessage netMsg)
	{
		Debug.Log("Received test message");
	}

	// Token: 0x060003FA RID: 1018 RVA: 0x00021112 File Offset: 0x0001F312
	public override void OnServerReady(NetworkConnection conn)
	{
		base.OnServerReady(conn);
	}

	// Token: 0x060003FB RID: 1019 RVA: 0x0002111B File Offset: 0x0001F31B
	public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
	{
		Debug.Log("on server add player: " + playerControllerId);
		base.OnServerAddPlayer(conn, playerControllerId);
	}

	// Token: 0x060003FC RID: 1020 RVA: 0x0002113A File Offset: 0x0001F33A
	public override void OnServerConnect(NetworkConnection conn)
	{
		base.OnServerConnect(conn);
		NetworkServer.RegisterHandler(global::MsgType.OtherTestMessage, new NetworkMessageDelegate(this.OnTestMessage));
	}

	// Token: 0x060003FD RID: 1021 RVA: 0x00021159 File Offset: 0x0001F359
	public override void OnClientConnect(NetworkConnection conn)
	{
		base.OnClientConnect(conn);
		this.RegisterHandlerClient(global::MsgType.OtherTestMessage, new NetworkMessageDelegate(this.OnTestMessage));
	}
}
